package com.example.lib_mvp.arouter;

import android.net.Uri;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RouterUtils {
    /**
     * 简单跳转，无参数
     *
     * @param path 路由路径
     */
    public static Object getNavigate(String path) {

        return ARouter.getInstance().build(path).navigation();

    }

    /**
     * 简单跳转，无参数
     *
     * @param path 路由路径
     */
    public static void navigate(String path) {
        try {
            ARouter.getInstance().build(path).navigation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 携带参数跳转
     *
     * @param path   路由路径
     * @param params 参数集合
     */
    public static void navigate(String path, Map<String, Object> params) {
        try {
            Postcard postcard = ARouter.getInstance().build(path);
            if (params != null && !params.isEmpty()) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();

                    if (value instanceof String) {
                        postcard.withString(key, (String) value);
                    } else if (value instanceof Integer) {
                        postcard.withInt(key, (Integer) value);
                    } else if (value instanceof Boolean) {
                        postcard.withBoolean(key, (Boolean) value);
                    } else if (value instanceof Serializable) {
                        // 处理可序列化的实体类
                        postcard.withSerializable(key, (Serializable) value);
                    } else if (value instanceof List) {
                        // 处理 List 集合
                        List<?> list = (List<?>) value;
                        if (!list.isEmpty()) {
                            Object firstItem = list.get(0);
                            if (firstItem instanceof String) {
                                postcard.withStringArrayList(key, new ArrayList<>((List<String>) list));
                            } else if (firstItem instanceof Integer) {
                                postcard.withIntegerArrayList(key, new ArrayList<>((List<Integer>) list));
                            } else if (firstItem instanceof Serializable) {
                                // 处理 List<实体类>
                                postcard.withSerializable(key, (Serializable) list);
                            }
                            //
                            // 可根据需要添加更多集合元素类型的处理
                        }
                    }
                    // 可根据需要添加更多类型的处理
                }
            }
            postcard.navigation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 携带参数并设置回调的跳转
     *
     * @param path     路由路径
     * @param params   参数集合
     * @param callback 导航回调
     */
    public static void navigate(String path, Map<String, Object> params, NavigationCallback callback) {
        try {
            Postcard postcard = ARouter.getInstance().build(path);
            if (params != null && !params.isEmpty()) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    if (value instanceof String) {
                        postcard.withString(key, (String) value);
                    } else if (value instanceof Integer) {
                        postcard.withInt(key, (Integer) value);
                    } else if (value instanceof Boolean) {
                        postcard.withBoolean(key, (Boolean) value);
                    } else if (value instanceof Serializable) {
                        // 处理可序列化的实体类
                        postcard.withSerializable(key, (Serializable) value);
                    } else if (value instanceof List) {
                        // 处理 List 集合
                        List<?> list = (List<?>) value;
                        if (!list.isEmpty()) {
                            Object firstItem = list.get(0);
                            if (firstItem instanceof String) {
                                postcard.withStringArrayList(key, new ArrayList<>((List<String>) list));
                            } else if (firstItem instanceof Integer) {
                                postcard.withIntegerArrayList(key, new ArrayList<>((List<Integer>) list));
                            } else if (firstItem instanceof Serializable) {
                                // 处理 List<实体类>
                                postcard.withSerializable(key, (Serializable) list);
                            }
                            //
                            // 可根据需要添加更多集合元素类型的处理
                        }
                    }
                    // 可根据需要添加更多类型的处理
                }
            }
            postcard.navigation(null, callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转到外部浏览器
     *
     * @param url 外部链接
     */
    public static void navigateToBrowser(String url) {
        try {
            Uri uri = Uri.parse(url);
            ARouter.getInstance().build(uri).navigation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}